//
//  SingleDayViewController.m
//  MonitoringNetwork
//
//  Created by admin on 2017/11/23.
//  Copyright © 2017年 Shenzhen Xinwo Transport. All rights reserved.
//

#import "SingleDayViewController.h"
#import "MineViewController.h"
#import "OperationView.h"
#import "VehicleDistributionView.h"
#import "DrivingConditionView.h"
#import "AlarmConditionView.h"
#import "LeftView.h"
#import "TitieView.h"
#import "MJExtension.h"
#import "OwnerModel.h"
#import "UseModel.h"
#import "CityModel.h"
#import "ProvinceModel.h"
#import "OwnerChilderModel.h"
#import "NSDateFormatter+Category.h"
#import "NSString+File.h"
@interface SingleDayViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)LeftView *leftView;
@property (nonatomic,strong)OperationView *operationView;
@property (nonatomic,strong)VehicleDistributionView *vehicleDistributionView;
@property (nonatomic,strong)DrivingConditionView *drivingConditionView;
@property (nonatomic,strong)AlarmConditionView *alarmConditionView;
@property (nonatomic,strong)UIScrollView *contentScrollView;
@property (nonatomic,strong)TitieView *titleView;
@property (nonatomic,strong)UIView *canvasView;
@property (nonatomic,strong)NSDictionary *paramDic;
@property (nonatomic,assign)NSInteger nowIndex;
@property (nonatomic,assign)BOOL needRefresh;
@property (nonatomic,assign)NSInteger currentIndex;
@end
@implementation SingleDayViewController
#pragma mark - 懒加载
- (NSDictionary *)paramDic
{
    if (!_paramDic) {
        _paramDic = [NSDictionary dictionary];
    }
    return _paramDic;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigationTitleWithTitle:@"单日统计"];
    [self loadUI];
    [self setupUIScrollView];
    _nowIndex = 0;
    _currentIndex = 100;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(allowSingleRefresh) name:@"allowSingleRefresh" object:nil];
    
    [self loadSbuViewDataWithIndex:0];


    [_operationView loadDataBackGroundWithParam:_paramDic];
    [_vehicleDistributionView loadDataBackGroundWithParam:_paramDic];
    [_drivingConditionView loadDataWithParam:_paramDic];
    [_alarmConditionView loadDataWithParam:_paramDic];

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
   
}
- (void)allowSingleRefresh
{
    _operationView.needRefresh = YES;
    _vehicleDistributionView.needRefresh = YES;
    _drivingConditionView.needRefresh = YES;
    _alarmConditionView.needRefresh = YES;
    
    _operationView.param = _paramDic;
    _vehicleDistributionView.param = _paramDic;
    _drivingConditionView.param = _paramDic;
    _alarmConditionView.param = _paramDic;
    
    [_operationView loadDataBackGroundWithParam:_paramDic];
    [_vehicleDistributionView loadDataBackGroundWithParam:_paramDic];
    [_drivingConditionView loadDataWithParam:_paramDic];
    [_alarmConditionView loadDataWithParam:_paramDic];
    
}
- (UIView *)canvasView
{
    if (!_canvasView) {
        _canvasView  =[UIView new];
        _canvasView.frame = CGRectMake(0, 0, kScreenWidth * 4, kScreenHeight-NavationBarHeight-40);
    }
    return _canvasView;
}
- (OperationView *)operationView
{
    if (!_operationView) {
        _operationView = [[OperationView alloc] init];
    }
    return _operationView;
}
- (VehicleDistributionView *)vehicleDistributionView
{
    if (!_vehicleDistributionView) {
        _vehicleDistributionView = [[VehicleDistributionView alloc] init];
    }
    return _vehicleDistributionView;
}
- (DrivingConditionView *)drivingConditionView
{
    if (!_drivingConditionView) {
        _drivingConditionView = [[DrivingConditionView alloc] init];
    }
    return _drivingConditionView;
}
- (AlarmConditionView *)alarmConditionView
{
    if (!_alarmConditionView) {
        _alarmConditionView = [[AlarmConditionView alloc] init];
    }
    return _alarmConditionView;
}
- (UIScrollView *)contentScrollView
{
    if (!_contentScrollView) {
        _contentScrollView = [[UIScrollView alloc] init];
        _contentScrollView.scrollEnabled = NO;
        _contentScrollView.frame = CGRectMake(0, NavationBarHeight+50, kScreenWidth, kScreenHeight-TabBarHeight-NavationBarHeight-50);
        _contentScrollView.contentSize = CGSizeMake(kScreenWidth * 4, 0);
        _contentScrollView.backgroundColor = [UIColor whiteColor];
        _contentScrollView.pagingEnabled = YES;
        _contentScrollView.bounces = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.delegate = self;
    }
    return _contentScrollView;
}
- (TitieView *)titleView
{
    if (!_titleView) {
        _titleView = [[TitieView alloc] init];
    }
    return _titleView;
}
- (void)loadUI{
    //设置导航栏左右按钮
    [self setLeftBarButtonItemWithNormalImageName:@"screeNormal" andSelectImageName:@"screeSel"];
    [self setRightBarButtonItemWithNormalImageName:@"personNormal" andSelectImageName:@"personSel"];

    Kweaself
    [self.view addSubview:self.titleView];
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(self.view.mas_top).offset(NavationBarHeight);
    }];
    [weakSelf.titleView setTitleSelectIndex:^(NSInteger index) {
        [weakSelf SectionSelectIndex:index];
    }];
    [self.titleView changeSelectItemWithIndex:0];
    
}
//设置滑动scrollView
- (void)setupUIScrollView
{
    [self.view addSubview:self.contentScrollView];
    [self.contentScrollView addSubview:self.canvasView];
    [self.canvasView addSubview:self.operationView];
    
    [self.operationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_canvasView.mas_left).offset(kScreenWidth*0);
        make.bottom.top.mas_equalTo(_canvasView);
        make.width.mas_equalTo(kScreenWidth);
    }];
    
    [self.canvasView addSubview:self.vehicleDistributionView];
    [self.vehicleDistributionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_contentScrollView.mas_left).offset(kScreenWidth*1);
        make.bottom.top.mas_equalTo(_canvasView);
        make.width.mas_equalTo(kScreenWidth);
    }];
    
    [self.canvasView addSubview:self.drivingConditionView];
    [self.drivingConditionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_contentScrollView.mas_left).offset(kScreenWidth*2);
        make.bottom.top.mas_equalTo(_canvasView);
        make.width.mas_equalTo(kScreenWidth);
    }];
    
    [self.canvasView addSubview:self.alarmConditionView];
    [self.alarmConditionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_contentScrollView.mas_left).offset(kScreenWidth*3);
        make.bottom.top.mas_equalTo(_canvasView);
        make.width.mas_equalTo(kScreenWidth);
    }];
}
#pragma mark - navigationBar点击事件
- (LeftView *)leftView
{
    if (!_leftView) {
        _leftView = [LeftView new];
        [self.navigationController.view addSubview:_leftView];
        [_leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.view).offset(StatuBarHeight);
            make.left.right.bottom.mas_equalTo(self.view);
        }];

        
    }
    return _leftView;
    
}
//左按钮点击事件
- (void)leftItemBtnClick:(UIButton *)sender
{
    [super leftItemBtnClick:sender];
    Kweaself
    [weakSelf jumpProhibit];
    self.tabBarController.tabBar.hidden = YES;
    [self.leftView show];
    self.leftView.viewDisapper = ^{
    weakSelf.tabBarController.tabBar.hidden = NO;
    };
    self.leftView.searchDidClick = ^(NSDictionary *param) {
        weakSelf.paramDic= param;
        [weakSelf setLeftBarButtonItemWithNormalImageName:@"screeSel" andSelectImageName:@"screeSel"];
        [weakSelf loadStatisticalDataWithDictionary:param];
    };
}
//右按钮点击事件
- (void)rightItemBtnClick:(UIButton *)sender
{
    [super rightItemBtnClick:sender];
    sender.enabled = NO;
    MineViewController *mineVc = [MineViewController new];
    [mineVc setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:mineVc animated:YES];
    sender.enabled = YES;
}
//发通知禁止tabBar跳转
- (void)allowProhibit
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"allowProhibit" object:nil];
}
//发通知允许tabBar跳转
- (void)jumpProhibit
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"jumpProhibit" object:nil];
}
#pragma mark - 分栏滑动换页效果

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    [self.titleView changeSelectItemWithIndex:index];
    if (_currentIndex == index) {
        return;
    }
    _currentIndex = index;
    _nowIndex = index;
    [self loadSbuViewDataWithIndex:index];
}

- (void)SectionSelectIndex:(NSInteger)selectIndex {
    CGFloat offsetX = selectIndex * self.view.frame.size.width;
    self.contentScrollView.contentOffset = CGPointMake(offsetX, 0);
     _nowIndex = selectIndex;
    [self loadSbuViewDataWithIndex:selectIndex];
}

#pragma mark - 数据加载
- (void)loadSbuViewDataWithIndex:(NSInteger)index
{

    _currentIndex  =index;
    switch (index) {
        case 0:
            _operationView.param = _paramDic;
            [_operationView loadDataWithParam:_paramDic];
            break;
        case 1:
            _vehicleDistributionView.param = _paramDic;
            [_vehicleDistributionView loadDataWithParam:_paramDic];
            break;
        case 2:
            _drivingConditionView.param = _paramDic;
            [_drivingConditionView loadDataWithParam:_paramDic];
            break;
        case 3:
            _alarmConditionView.param = _paramDic;
            [_alarmConditionView loadDataWithParam:_paramDic];
            break;
        default:
            break;
    }
}
- (void)firstLoadAllData
{
        
}
- (void)loadStatisticalDataWithDictionary:(NSDictionary *)dic
{
    _paramDic = dic;
    [self loadSbuViewDataWithIndex:_nowIndex];
}
@end
